package com.power.algorithm.test;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/21 2:15 下午
 * <p>
 * <p>
 * 暴力破解字符串匹配的问题
 * 一一对应  如果不匹配在回退到开始的位置进行
 */

public class ViolenceMatching {

    public int match(String originStr, String patternStr) {
        char origin[] = originStr.toCharArray();
        char pattern[] = patternStr.toCharArray();
        int originLen = origin.length;
        int patternLen = pattern.length;
        int i = 0;
        int j = 0;
        while (i < originLen || j < patternLen) {
            if (origin[i] == pattern[j]) {
                i++;
                j++;
            } else {
                i = i - j + 1;
                j = 0;
            }
        }
        if (j == patternLen) {
            return i - j + 1;
        } else {
            return -1;
        }
    }

    public static void main(String[] args) {
        String originStr = "今天的太阳真好,美好的一天";
        String patternStr = "美好的一天";

        ViolenceMatching violenceMatching = new ViolenceMatching();
        System.out.println(violenceMatching.match(originStr, patternStr));

    }

}
